﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Compression;
using System.IO;
using System.Threading;

namespace XBoshiGFS.forms
{
    public partial class BackUpAndRestorForm : Form
    {

        String stationName = "";
        public BackUpAndRestorForm()
        {
            InitializeComponent();

            using (XBoshiGFSModelContainer context = new XBoshiGFSModelContainer()) {
                var sql = from p in context.StationSet select p;
                try
                {
                    stationName = sql.FirstOrDefault().strName;
                }
                catch { }
            }


        }

        private void btnBackUp_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrWhiteSpace(TBFileName.Text)) {
                MessageBox.Show("请输入保存文件名称", "提示");
                   return ;
            }
            //开始备份数据
            try
            {
                String DBFileName = Application.StartupPath  + "/XboshiGFS.sdf";
                FileStream fios =new  FileStream(DBFileName, FileMode.Open);
                long filesize = fios.Length;
                progressBar1.Maximum = (int)filesize/1024;
                GZipStream gzs = new GZipStream(  backupDilaog.OpenFile(),CompressionMode.Compress );
               byte[]  b=new byte[1024];
               int read = 0;
               while((read=fios.Read (b,0,1024))!=0){
                    gzs.Write(b,0,read);
                    progressBar1.Value += read/1024;
                    Thread.Sleep(0); 
               }
                fios.Close();
                gzs.Close ();
                progressBar1.Value=progressBar1.Maximum;
                MessageBox.Show("数据备份成功！请保存好备份文件！", "提示");
            }catch(Exception ex){
                MessageBox.Show ("数据备份失败！", "提示");
                return;
            } 

         }

        private void btnSaveAs_Click(object sender, EventArgs e)
        {
            backupDilaog.Filter = "备份文件(*.bck)|*.bck";
            backupDilaog.InitialDirectory = Application.StartupPath + "/back"; 
            backupDilaog.FileName = String.Format("{1}_{0:yyyyMMdd}.bck", DateTime.Now, stationName);

            if (backupDilaog.ShowDialog()==DialogResult.OK ) {
                TBFileName.Text  = backupDilaog.FileName; 
            }             
        }

        private void btnRecover_Click(object sender, EventArgs e)
        {
            String recoverFileName = TBRecoverFileName.Text;

            if (String.IsNullOrWhiteSpace(recoverFileName))
            {
                MessageBox.Show("请输入保存文件名称", "提示");
                return;
            }

            if (File.Exists(recoverFileName)==false ) {
                MessageBox.Show("请指定的文件不存在，重新选择备份文件", "提示");
                return;
            }


            String DBFileName = "";
            String DBBackFile = Guid.NewGuid().ToString();   
            //开始备份数据
            try
            {
                Cursor = Cursors.WaitCursor; 
                
                DBFileName = Application  .StartupPath + "/XboshiGFS.sdf";
                if (!File.Exists(DBFileName)) { 
                    //备份文件不存在
                    DBFileName = Application.LocalUserAppDataPath + "/XboshiGFS.sdf";
                }



                File.Move(DBFileName, DBBackFile);
                
                
                FileStream fios = new FileStream(DBFileName, FileMode.OpenOrCreate);
                FileStream backfile = File.OpenRead(recoverFileName);
                GZipStream gzs = new GZipStream(backfile, CompressionMode.Decompress);
                byte[] b = new byte[1024];
                int read = 0;
                long filesize = backfile.Length;
                while ((read = gzs.Read(b, 0, 1024)) != 0)
                {
                    fios.Write(b, 0, read);
                    Thread.Sleep(0);
                }
                fios.Close();
                gzs.Close();
                backfile.Close();
                File.Delete(DBBackFile);
                MessageBox.Show("数据恢复成功！请重新登录！", "提示");
            }
            catch (Exception ex)
            {
                MessageBox.Show("数据恢复失败！", "提示");
                //恢复文件
                File.Move(DBBackFile, DBFileName);
                return;
            }

            Cursor = Cursors.Default ;
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            openFileDialog1.Filter = "备份文件(*.bck)|*.bck";
            openFileDialog1.InitialDirectory = Application.StartupPath + "/back";
            openFileDialog1.FileName = "";

            if ( openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                TBRecoverFileName.Text = openFileDialog1.FileName;
            }    
        }


        public void SetTabPage(int pageNo) {
            this.tabControl1.SelectedIndex=pageNo;   
        }

 
    }
}
